package com.tech.doggtv.fragment;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * @author jl_luo
 * @name: com.tech.doggtv.fragment
 * @description:
 * @date 2015/9/14 16:31
 */

public abstract class BaseFragment extends Fragment{
    //主view
    protected View mContentView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mContentView == null) {
            initView(savedInstanceState);
        } else {
            ViewGroup parent = (ViewGroup) mContentView.getParent();
            if (parent != null) {
                parent.removeView(mContentView);
            }
        }
        ButterKnife.inject(this, mContentView);
        return mContentView;
    }

    protected void setContentView(@LayoutRes int layoutResID) {
        mContentView = LayoutInflater.from(getActivity()).inflate(layoutResID, null);
        ButterKnife.inject(this,mContentView);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            onUserVisible();
        }
    }

    /**
     * 初始化View控件
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
    * 当fragment对用户可见时，会调用该方法，可在该方法中懒加载网络数据
    */
    protected abstract void onUserVisible();

    protected  void resetButterKnife(){
        ButterKnife.reset(this);
    }
}
